package com.procedures;


import java.io.File;
import com.Const;



import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;


public class EnterJdbcJsp {
	
	 com.PublicSystem sys=com.PublicSystem.getInstance();
	

	private String[] colnames; // 列名数组
	private String[] colTypes; //列名类型数组
	private int[] colSizes; //列名大小数组
	String res="0";

	
	public String createJsp(String tablename,String vopage,String index) throws Exception{
		getDb(tablename);
		res=createJspForQuery( tablename, vopage,index);
		if("0".equals(res))res=createJspForModify(tablename, vopage,index);
		return res;
	}
	
	public String createJspForQuery(String tablename,String vopage,String index){
    	
        		  String getUrl=Const.path+"/WebRoot/page/"+tablename;
        		  String getUrlJsp=Const.path+"/WebRoot/page/"+tablename+"/"+tablename+".jsp";
        		  StringBuffer sb = new StringBuffer();
        		  try
        		  {
        			  //查询模板
        			  File sourceFile = new File(Const.path+"/src/com/jspmodel/query_model.jsp");//已经存在的模板文件
        			  File targetFile = new File(getUrl);//需要建立的新文件
        			  if (!targetFile.exists()) {
        				  targetFile.mkdirs();
        			  }
				  
        			  InputStreamReader read = new InputStreamReader(new FileInputStream(sourceFile),"UTF-8"); 
        			  BufferedReader br = new BufferedReader(read);
				 
				  
        			  OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(getUrlJsp),"UTF-8"); 
        			  BufferedWriter bw = new BufferedWriter(write);
        			  String str = null;
        			  
        			  for(int j=0;j<colnames.length;j++)
    				  {
    					  
    						  sb.append("       <div field=\""+colnames[j].toLowerCase()+"\" width=\"120\" headerAlign=\"center\" allowSort=\"true\">"+colnames[j]+"</div>\r\n");
    					  
    				  }
        			  
        			  while( (str = br.readLine()) != null){
					
        				  if(str.contains("system.do")) str = str.replaceAll("system.do", ""+tablename+".do");
        				  if(str.contains("getmenu")) str = str.replaceAll("getmenu", "get"+tablename);
        				  if(str.contains("page/company/system/menu_edit.jsp"))str = str.replaceAll("page/company/system/menu_edit.jsp", "page/"+tablename+"/"+tablename+"_edit.jsp");
        				  if(str.contains("removemenu"))str = str.replaceAll("removemenu", "remove"+tablename);
        				  if(str.contains("row.id"))str = str.replaceAll("row.id", "row."+index);
        				  if(str.contains("r.id"))str = str.replaceAll("r.id", "r."+index);
        				  if(str.contains("<listgjx>"))str = str.replaceAll("<listgjx>", sb.toString());
        				  
        				  bw.write(str);
        				  bw.newLine(); 
        			  }
				  bw.flush();
				  bw.close();
				
				  System.out.println(Const.path+"/WebRoot/page/"+tablename+"/"+tablename+".jsp已经创建成功");
				
			} catch (IOException e) {
				e.printStackTrace();
				res="1";
			}
		return res;
    }
	
	
	
	
	
	public String createJspForModify(String tablename,String vopage,String index){
    	
		  String getUrl=Const.path+"/WebRoot/page/"+tablename;
		  String getUrlJsp=Const.path+"/WebRoot/page/"+tablename+"/"+tablename+"_edit.jsp";
		  StringBuffer sb = new StringBuffer();
		  try
		  {
			  //查询模板
			  File sourceFile = new File(Const.path+"/src/com/jspmodel/edit_model.jsp");//已经存在的模板文件
			  File targetFile = new File(getUrl);//需要建立的新文件
			  if (!targetFile.exists()) {
				  targetFile.mkdirs();
			  }
		  
			  InputStreamReader read = new InputStreamReader(new FileInputStream(sourceFile),"UTF-8"); 
			  BufferedReader br = new BufferedReader(read);
		 
		  
			  OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(getUrlJsp),"UTF-8"); 
			  BufferedWriter bw = new BufferedWriter(write);
			  String str = null;
			  String dataType="";String dataClass="";
			  
			  sb.append("<tr>\r\n");
			  for(int j=0;j<colnames.length;j++)
			  {
				  dataType="";dataClass="mini-textbox";
				  if("NUMBER".equals(colTypes[j]))dataType="vtype=\"int\"";
				  if("DATE".equals(colTypes[j]))
				   {
					  dataType="vtype=\"date:yyyy-MM-dd\"";dataClass="mini-datepicker";
				   }
				  //System.out.println(colTypes[j]);
				  sb.append("<td style=\"width:20%;\">"+colnames[j]+"：</td>\r\n");
				  sb.append("<td style=\"width:30%;\"><input name=\""+colnames[j].toLowerCase()+"\" class=\""+dataClass+"\" required=\"true\" "+dataType+"  emptyText=\"请输入"+colnames[j]+"\"/></td>\r\n");
				  if(j%2==0)
				  {
					 if(j==colnames.length-1)sb.append("</tr>\r\n");
					 else sb.append("</tr>\r\n<tr>\r\n");
				  }
			  }
			  
			  
			  
			  
			  while( (str = br.readLine()) != null){
			
				  if(str.contains("/wondermini/system.do")) str = str.replaceAll("/wondermini/system.do", "/"+Const.project+"/"+tablename+".do");
				  if(str.contains("savemenu")) str = str.replaceAll("savemenu", "save"+tablename);
				  if(str.contains("getmenudata"))str = str.replaceAll("getmenudata", "get"+tablename+"data");
				  
				  if(str.contains("<gjx>")){
					  str = str.replaceAll("<gjx>", sb.toString());
				  }
				  
				  
				  bw.write(str);
				  bw.newLine(); 
			  }
		  bw.flush();
		  bw.close();
		
		  System.out.println(Const.path+"/WebRoot/page/"+tablename+"/"+tablename+"_edit.jsp已经创建成功");
		
	} catch (IOException e) {
		e.printStackTrace();
		res="1";
	}
return res;
}

	
	
	private void getDb(String tablename) throws Exception
	{
		Connection con=null;
		
		//查要生成实体类的表
    	String sql = "select * from " + tablename;
    	Statement pStemt = null;

    		try {
    			con=sys.getConnection();
    			pStemt = (Statement) con.createStatement();  
    			ResultSet rs = pStemt.executeQuery(sql);  
    			ResultSetMetaData rsmd = rs.getMetaData();  

    			int size = rsmd.getColumnCount();	//统计列
    			colnames = new String[size];
    			colTypes = new String[size];
    			colSizes = new int[size];
    			for (int i = 0; i < size; i++) {
    				colnames[i] = rsmd.getColumnName(i + 1);
    				colTypes[i] = rsmd.getColumnTypeName(i + 1);
    				colSizes[i] = rsmd.getColumnDisplaySize(i + 1);
    				
    			}
			}catch(Exception f)
			{
				f.printStackTrace();
				res="1";
    		} finally{
    			try
    			{
    				pStemt.close();
    				con.close();
    			}
    			catch (SQLException e) {
    				e.printStackTrace();
    			} 
			
    		}
	}
	
	

}